import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		noHistory: "noHistoryHide",
		noData: "noData",  //noData无数据   
		contentList: [],
		totalPages: 1,
		pageIndex: 1,
		showHistory: false,
		buttonName: "视频记录",
	},
	newsDetail(event) {
		wx.navigateTo({
			url: '../newsDetail/newsDetail?assetId=123&folderId=456'
		})
	},
	articleDetail(e) {
		// var folderCode=e.currentTarget.dataset.foldercode;
		var folderCode = 0;
		var type = e.currentTarget.dataset.type;
		var code = e.currentTarget.dataset.code;
		var name = e.currentTarget.dataset.name;
		var detailUrl = "";
		detailUrl = this.getUrl(type, code, folderCode, name);
		if (detailUrl.length > 0) {
			wx.navigateTo({
				url: detailUrl
			});
		} else {
			this.showPageMsg("tips", "该内容类型详情页暂未开发");
		}
	},
	onLoad() {
		this.loadHistory();
	},
	onReachBottom() {
		// 页面被拉到底部
		if (this.data.pageIndex > this.data.totalPages) {
			this.showPageMsg("tips", "没有更多内容了");
		} else {
			this.loadHistory();
		}
	},
	onPullDownRefresh: function() {
		this.setData({
			pageIndex: 1,
			contentList: []
		});
		this.loadHistory();
		wx.stopPullDownRefresh();
	},
	loadHistory: function() {
		var newUrl = getApp().globalData.dangjianUrl + "/member/userPlayHistory";
		var params = {};
		params["from"] = "";
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = "10";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "历史记录加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var contents = respData.data.contents.content;
					var pageIndex = respData.data.contents.pageIndex + 1;
					var totalPages = respData.data.contents.totalPages;
					var contentList = [];
					for (var index in contents) {
						var content = contents[index];
						var json = {};
						var thumbnail = content.thumbnail;
						if (null == thumbnail || "" == thumbnail) {
							var imageFiles = content.imageItems;
							for (var j in imageFiles) {
								thumbnail = imageFiles[j].url;
								break;
							}
						}
						json["name"] = content.name;
						if (content.duration >= content.timeLong) {
							json["compelte"] = '已完成';
						} else {
							json["compelte"] = '未完成';
						}
						json["url"] = thumbnail;
						json["code"] = content.code;
						json["type"] = content.type;
						json["sourceFrom"] = null == content.sourceFrom ? "华数" : content.sourceFrom;
						contentList.push(json);
					}
					var oldContentList = this.data.contentList;
					contentList = oldContentList.concat(contentList);
					this.setData({
						"contentList": contentList,
						"pageIndex": pageIndex,
						"totalPages": totalPages,
						"showHistory": contentList.length > 0 ? false : true,
					});
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	}
});
